var orderId=getUrlVal("orderId");
$(function(){
   getOrderDetails()
});
function getOrderDetails(){
    $(".allOrderUl").html("");
    $("#loadingModal").modal();
    ym.ajax({
        url:'/shop/order/orderDetails',
        param:{
            orderid:orderId,
        },
        success:function(data){
            console.log(data);
            $("#shopOrderUserName").html(data.obj.shopOrderUserName);
            $("#tel").html(data.obj.shopOrderUserMobile);
            $(".addrMess span").html(data.obj.userReceivingAddress);
            $("#orderId").html(data.obj.shopOrderFlowno);
            $("#showOrderTime").html(data.obj.showOrderTime);
            if(data.obj.shopOrderStatus=="theEnd"||data.obj.shopOrderStatus=="pendingGoods"){
                if(data.obj.shopOrderCompany!=null&&data.obj.shopOrderCompany!=undefined){
                    $(".shopOrderCompany").html(data.obj.shopOrderCompany)
                }else{
                    $(".shopOrderCompany").html("暂无")
                }
                if(data.obj.shopOrderWaybillNumber!=null&&data.obj.shopOrderWaybillNumber!=undefined){
                    $(".shopOrderWaybillNumber").val(data.obj.shopOrderWaybillNumber)
                    $(".copySpan").removeClass("hide")
                }else{
                    $(".shopOrderWaybillNumber").val("暂无");
                    $(".copySpan").addClass("hide")
                }
                $(".hideLi").removeClass("hide");
            }else{
                $(".hideLi").addClass("hide");
            }
            if(data.obj.payType==0){//积分
                $("#price").html(data.obj.shopOrderPoint+"积分")
            }else if(data.obj.payType==1){//金钱
                var money=(data.obj.shopOrderTotalcost*0.01).toFixed(2);
                console.log(money);
                $("#price #moneys").html(money);
                $("#price #interage").html("+"+data.obj.shopOrderPoint+"积分");
            }
            var arrPro=data.obj.orderProductVos;
            var html="";
            var allNum=0;
            for(var i=0;i<arrPro.length;i++){
                var k=arrPro[i];
                html+='<li><span class="goodsDiv">'
                html+='<span class="goodsImgDiv">'
                html+='<img src="'+k.proPic+'" class="goodsImg" />'
                html+='</span>'
                html+='<span class="goodsMessDivs">'
                html+='<span class="goodName">'
                html+='<span class="goodsName">'+k.name+'</span>'
                html+='</span>'
                html+='<span class="goodsNumDiv">'
                if(k.payMethod==0){//0积分，1金钱
                    html+='<span class="goodMoneyDiv ar-left">'+k.point+'积分</span>'
                }else if(k.payMethod==1){
                    var price=(k.price*0.01).toFixed(2);
                    html+='<span class="goodMoneyDiv ar-left">'+price+'（'+k.point+'积分）</span>'
                }
                html+='<span class="goodsNum ar-right">X '+k.number+'</span>'
                allNum+=k.number;
                html+='</span></span></span></li>'
            }
            $("#num").html(allNum)
            $(".allOrderUl").append(html);
            var html1="";
            if(data.obj.shopOrderStatus=="pendingPayment"){//待付款
                html1+='<button class="hasColorBtn btnBackCor" onclick="goBuy()">去付款</button>'
                html1+='<button class="noColorBtn" onclick="delOrder('+data.obj.shopOrderId+',0)">取消订单</button>'
            }else if(data.obj.shopOrderStatus=="pendingDelivery"){//待发货
                html1+='<button class="noColorBtn">待发货</button>'
            }else if(data.obj.shopOrderStatus=="pendingGoods"){//待收货
                html1+='<button class="hasColorBtn btnBackCor" onclick="delOrder('+data.obj.shopOrderId+',1)">确认收货</button>'
            }else if(data.obj.shopOrderStatus=="theEnd"){//已完成
                html1+='<button class="noColorBtn" onclick="delOrder('+data.obj.shopOrderId+',0)">删除订单</button>'
            }
            $(".opBtnDiv").append(html1);
            $("#loadingModal").modal('close');
        }
    });
}
function delOrder(orderId,type){
    if(type==0){//删除
        $('#myConfirm .am-modal-bd').html("您是否确定删除此订单？");
        var update="";
    }else if(type==1){
        $('#myConfirm .am-modal-bd').html("您是否确认收货？");
        var update=2;
    }
    var $confirm = $('#myConfirm');
    var confirm = $confirm.data('amui.modal');
    var onConfirm = function() {
        //点击确认按钮调用事件
        ym.ajax({
            url:'/shop/order/del',
            param:{
                orderId:orderId,
                update:update,
            },
            success:function(data){
                console.log(data)
                if(data.res==1){
                    $("#alertModal .am-modal-bd").html("操作成功！");
                    $("#alertModal").modal();
                    setTime();
                }
            }
        });

    };
    var onCancel = function() {
        //取消按钮调用事件
    };
    if (confirm) {
        confirm.options.onConfirm =  onConfirm;
        confirm.options.onCancel =  onCancel;
        confirm.toggle(this);
    } else {//重定义模态窗函数
        $confirm.modal({
            relatedElement: this,
            closeViaDimmer: false,//禁止点击遮罩层关闭
            onConfirm: onConfirm,
            onCancel: onCancel
        });
    }
}
/*两秒后关闭弹窗*/
function setTime(){
    setTimeout(function () {
        $("#alertModal").modal('close');
        window.location.href="order.html";
    }, 2000);
}
/*复制运单号*/
function copy(){
    var input = document.getElementById("inputNumber");
    input.select();
    document.execCommand("copy");
    $("#alertModal .am-modal-bd").html("复制成功！");
    $("#alertModal").modal();
    setTimeout(function () {
        $("#alertModal").modal('close');
    }, 2000);
}
//去付款
function goBuy(){
    var totalFee=$("#price #moneys").html();
    var point=$("#price #interage").html();
    var nums=0;
    $('#myConfirm .am-modal-bd').html("您是否确认花费"+totalFee+"元+"+point+"购买此商品？");
    var $confirm = $('#myConfirm');
    var confirm = $confirm.data('amui.modal');
    var onConfirm = function() {
        nums++;
        $(".masks").removeClass("hide");
        if(nums==1){
            totalFee *= 100;
            //调支付接口
            payType(orderId,totalFee);
        }
    };
    var onCancel = function() {
        //取消按钮调用事件
    };
    if (confirm) {
        confirm.options.onConfirm =  onConfirm;
        confirm.options.onCancel =  onCancel;
        confirm.toggle(this);
    } else {//重定义模态窗函数
        $confirm.modal({
            relatedElement: this,
            closeViaDimmer: false,//禁止点击遮罩层关闭
            onConfirm: onConfirm,
            onCancel: onCancel
        });
    }
}
